<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //
    protected $guarded = [];


    public function author()
    {
        return $this->belongsTo('App\User', 'userid');
    }

    public function favBy()
    {
        return $this->belongsToMany('App\User','studyfavs','studyid','userid')->where('type', '<' , 100);
    }

    public function favCount()
    {
        return $this->favBy()->count();
    }

    public function scopeUndelete($query)
    {
        return $query->whereDelete(0);
    }

    public function getByTagAndOrder($tag, $order,$limit=20)
    {
        if($tag != 'all')
            return $this->where('tag',$tag)->undelete()->orderBy($order,'desc')->paginate($limit);
        else
            return $this->undelete()->orderBy($order,'desc')->paginate($limit);
    }



}
